package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.user.dtos.WmMaterialDto;
import com.heima.wemedia.pojos.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(("/api/v1/material"))
public class MaterialController {

    @Autowired
    private WmMaterialService materialService;

    @PostMapping("/upload_picture")
    public ResponseResult addMaterial(MultipartFile multipartFile) {
        materialService.addMaterial(multipartFile);
        return ResponseResult.okResult(null);
    }

    /**
     * 素材管理 分页查询
     *
     * @param wmMaterialDto
     * @return
     */
    @PostMapping("/list")
    public PageResponseResult materialList(@RequestBody WmMaterialDto wmMaterialDto) {
        Page<WmMaterial> page = materialService.materialList(wmMaterialDto);
        PageResponseResult result = PageResponseResult.getPageResponseResult(page);
        return result;
    }

    /**
     * 删除图片
     *
     * @param id
     * @return
     */
    @GetMapping("/del_picture/{id}")
    public ResponseResult deletePicture(@PathVariable Integer id) {
        materialService.deletePicture(id);
        return ResponseResult.okResult(null);
    }

    /**
     * 取消收藏
     * @param id 图片id 1取消收藏
     * @return null
     */
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult cancelCollect(@PathVariable Integer id) {
        LambdaUpdateWrapper<WmMaterial> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(WmMaterial::getIsCollection, 0)
                .eq(WmMaterial::getId, id);
        materialService.update(wrapper);
        return ResponseResult.okResult(null);
    }

    /**
     * 收藏
     * @param id 图片id 0收藏
     * @return null
     */
    @GetMapping("/collect/{id}")
    public ResponseResult collect(@PathVariable Integer id) {
        LambdaUpdateWrapper<WmMaterial> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(WmMaterial::getIsCollection, 1)
                .eq(WmMaterial::getId, id);
        materialService.update(wrapper);
        return ResponseResult.okResult(null);
    }

}
